package com.sinsoft.monitorDemo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinsoft.monitorDemo.dao.CpuInfoDao;
import com.sinsoft.monitorDemo.dao.ServerDao;
import com.sinsoft.monitorDemo.entity.CpuInfo;
import com.sinsoft.monitorDemo.entity.ServerInfo;

@Controller
@RequestMapping("/cpuInfo")
public class CpuInfoController {
	
	/**
	 * 得到当前时间之前一段时间的的CPU信息
	 * @return
	 */
	@RequestMapping(value = "/getCpuinfoList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getCpuinfoList(HttpServletRequest request){
		CpuInfoDao dao = new CpuInfoDao();
		String ip = request.getParameter("ip");
		List<CpuInfo> cpuInfos = dao.getCpuInfoList(ip);
		ResponseData responseData = new ResponseData(true, cpuInfos);
		return responseData;	
	}
	
	/**
	 * 得到最新的CPU信息
	 * @return
	 */
	@RequestMapping(value = "/getNewCpuinfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewCpuinfo(HttpServletRequest request){
		CpuInfoDao dao = new CpuInfoDao();
		String ip = request.getParameter("ip");
		CpuInfo cpuInfo = dao.getNewCpuInfo(ip);
		ResponseData responseData = new ResponseData(true, cpuInfo);
		return responseData;	
	}
	
	/**
	 * 得到最新的CPU信息
	 * @return
	 */
	@RequestMapping(value = "/getAllCpuinfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllCpuinfo(HttpServletRequest request){
		CpuInfoDao dao = new CpuInfoDao();
		ServerDao serverDao = new ServerDao();
		Map<String, CpuInfo> cpuInfos = new HashMap<String, CpuInfo>();
		List<ServerInfo> serverInfoS = serverDao.getAllServerInfo();
		CpuInfo cpuInfo;
		for (ServerInfo serverInfo : serverInfoS) {
			String ip = serverInfo.getIp();
			if(ip.contains(",")){
				String[] split = ip.split(",");
				for (int i = 0; i < split.length; i++) {
					cpuInfo = dao.getNewCpuInfo(split[i]);
					cpuInfos.put(split[i], cpuInfo);
				}
			}else {
				cpuInfo = dao.getNewCpuInfo(ip);
				cpuInfos.put(ip, cpuInfo);				
			}
		}
		ResponseData responseData = new ResponseData(true, cpuInfos);
		return responseData;	
	}	
}
